xquery version "1.0";

import module namespace layout="http://www.example.com/layout" at "xmldb:exist:///db/projdb/modules/layout.xqm";
import module namespace request="http://exist-db.org/xquery/request";

declare option exist:serialize "doctype-public=-//W3C//DTD&#160;XHTML&#160;1.1//EN
                                doctype-system=http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";

declare variable $party {util:binary-to-string(xs:base64Binary(request:get-parameter("id", ())))};

declare variable $doc {doc('/db/projdb/participatie_parameters_ge2009.xml')};
declare variable $partyMembers {$doc/result/tupel[data(pparty) = $party]};
declare variable $numberOfMeetings {data($doc/result/stats/numberOfMeetings)};            

declare variable $pmList {<ul>{for $member in $partyMembers let $name := data($member/speaker)  where data($member/role) = 'mp' order by $name return <li><a href="individual.xql?id={data($member/@ref)}">{$name}</a></li>}</ul>};
declare variable $govList {<ul>{for $member in $partyMembers let $name := data($member/speaker)  where data($member/role) = 'government' order by $name return <li><a href="individual.xql?id={data($member/@ref)}">{$name}</a></li>}</ul>};
declare variable $chairList {<ul>{for $member in $partyMembers let $name := data($member/speaker)  where data($member/role) = 'chair' order by $name return <li><a href="individual.xql?id={data($member/@ref)}">{$name}</a></li>}</ul>};


declare variable $onderbroken {document('/db/projdb/onderbrokendoor.xml')/perpartij};
declare variable $perpartij {document('/db/projdb/perpartij.xml')/perpartij};
declare variable $partijen {document('/db/projdb/partijen.xml')/partijen};
declare variable $onderbrlistnaam {<ul>{for $j in $perpartij/entry[data(./naam) = $party]/data/entry/naam return <li><a href="party.xql?id={util:string-to-binary(xs:string(data($j)))}">{data($j)}</a></li>}</ul>};
declare variable $onderbrlistperc {<ul>{for $j in $perpartij/entry[data(./naam) = xs:string($party)]/data/entry/perc return <li>{data($j)}</li>} </ul>};
declare variable $onderbrlistnaamomg { <ul>{for $j in $onderbroken/entry[data(./naam) = $party]/data/entry/naam return <li><a href="party.xql?id={util:string-to-binary(xs:string(data($j)))}">{data($j)}</a></li>}</ul>};
declare variable $onderbrlistpercomg { <ul>{for $j in $onderbroken/entry[data(./naam) = xs:string($party)]/data/entry/perc return <li>{data($j)}</li>}</ul>};


declare variable $onderbrimage {
<img border="1" src="https://chart.googleapis.com/chart?chs=699x200&amp;chco={string-join(for $j in $perpartij/entry[data(./naam) = xs:string($party)]/data/entry/naam
                                                                                                             return $partijen/entry[./naam =  $j]/kleur,"|")}&amp;chd=t:{ string-join(for $j in $perpartij/entry[data(./naam) = xs:string($party)]/data/entry/perc
                                                                                                return $j,",") }&amp;cht=p&amp;chl={string-join(for $j in $perpartij/entry[data(./naam) = xs:string($party)]/data/entry/naam
                                                                                                return replace($j,'&amp;','%26'),"|")}"/>
};

declare variable $onderbrimageomg { 
<img border="1" src="https://chart.googleapis.com/chart?chs=699x200&amp;chco={string-join(for $j in $onderbroken/entry[data(./naam) = xs:string($party)]/data/entry/naam
                                                                                                             return $partijen/entry[./naam =  $j]/kleur,"|")}&amp;chd=t:{ string-join(for $j in $onderbroken/entry[data(./naam) = xs:string($party)]/data/entry/perc
                                                                                                return $j,",") }&amp;cht=p&amp;chl={string-join(for $j in $onderbroken/entry[data(./naam) = xs:string($party)]/data/entry/naam
                                                                                                return replace($j,'&amp;','%26'),"|")}"/>
};

declare variable $partyType {if ($partyMembers[data(role) = 'government']) then 'Regeringsparij' else 'Opositiepartij'};
declare variable $numberOfMan {count($partyMembers[data(sex) = 'M'])};
declare variable $numberOfWomen {count($partyMembers[data(sex) = 'F'])};
declare variable $numberOfMayors {count($partyMembers[data(mayor) = 'TRUE'])};
declare variable $numberOfNoneMayors {count($partyMembers[data(mayor) = 'FALSE'])};

declare variable $partyPresent {fn:round-half-to-even(xs:double(sum(data($partyMembers/present))) div xs:double(count($partyMembers/present)))};

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Activiteit in de plenaire vergaderingen</title>
        <link rel="stylesheet" style="text/css" href="style/core_layout.css"/>
		<script type="text/javascript" src="js/jquery.js"><!-- --></script>
        <script type="text/javascript" src="https://www.google.com/jsapi"><!-- --></script>
        <script type="text/javascript" src="js/activity.js"><!-- --></script>
        <script type="text/javascript" src="js/myScripts.js"><!-- --></script>
        <script type="text/javascript">
            var party = "{$party}";
            var individualPresent = {$partyPresent};
            var numberOfMeetings = {$numberOfMeetings};
            //<![CDATA[
            google.load("visualization", "1", {packages:["corechart"]});
            google.setOnLoadCallback(function() {
                initShowHideButtons();

                activity_setUpdateFunction(function(xml) {
                    activity_loadPartyQuartilePieChart(xml, "partyQuartileChartContainer", party);
                });

                activity_run();
                
                // begin: static charts
                var list = [['Aanwezigheden', 'Persoon'],
                            ['Aanwezig', individualPresent],
                            ['Niet-aanwezig', numberOfMeetings - individualPresent]];

                var data = google.visualization.arrayToDataTable(list);

                var options = {
                    title: "Gemiddelde aanwezigheid",
                    is3D: true,
                    colors: ['#005500', '#cccccc']
                };

                var chart = new google.visualization.PieChart(document.getElementById("presentContainer"));
                chart.draw(data, options);
                // end: static charts
                
            });
            //]]>
        </script>
        
        
    </head>
    <body>
        <div class="containerBox">
  
        {layout:topMenu()}
  
        <div class="box" id="titleBox">
            <div class="margin">
                <div id="partyInfoChartContainer">
                    <h1>{$party}</h1>
                     <strong>{$partyType}</strong>
                </div>
            </div>
        </div>

        <div class="box" id="partyQuartileChartBox">
            <div class="margin">
                <h2 class="boxTitle">Partij informatie</h2>
                <div class="boxContent columnspace twoColumns">
                    <div class="column">
                        <div class="field leftField">Mannen: #{$numberOfMan}</div>
                        <div class="field leftField">Vrouwen: #{$numberOfWomen}</div>
                    </div>
                    <div class="column">
                        <div class="field rightField">Burgemeesters: #{$numberOfMayors}</div>
                        <div class="field rightField">Niet-burgemeesters: #{$numberOfNoneMayors}</div>
                    </div>
                    <div class="column" id="presentContainer"><!-- --></div>  
                    <div class="column" id="partyQuartileChartContainer" style="height:200px;"><!-- Auto generated --></div>
                    <br class="clearer" />
                </div>
            </div>
        </div>

        <div class="box" id="paryMembersBox">
            <div class="margin">
                <h2 class="boxTitle">Leden</h2>
                <div class="boxContent darkBackground hideByDefault twoColumns columnspace">
                <div class="column">
                <h3 class="columnTitle">Volksvertegenwoordigers</h3>
                {$pmList}
                </div>
                <div class="column">
                {if (data($govList) != '') then 
                (<h3 class="columnTitle">Regeringsleden</h3>,
                $govList) else ()}
                {if (data($chairList) != '') then 
                    (<h3 class="columnTitle">Voorzitter</h3>,
                $chairList)
                else () }
                <!-- -->
                </div>

                <br class="clearer" />
                </div>
            </div>
        </div>
        
        <div class="box" id="onderbrekingBox">
            <div class="margin">
                <h2 class="boxTitle">De partij '{$party}' onderbreekt wie?</h2>
                <div class="boxDescription">
                <p>Hier wordt weergegeven hoeveel (in percentage) {$party} alle partijen onbreekt t.o.v. hoeveel de partij onderbreekt in totaal.</p>
                </div>
                <div class="boxContent darkBackground">
                    <div class="columnspace twoColumns darkBackground">
                        <div class="column firstColumn">
                            <h3 class="columnTitle">Naam</h3>
                            {$onderbrlistnaam}
                            </div>
                        <div class="column secondColumn">
                            <h3 class="columnTitle">Percentage</h3>
                              {$onderbrlistperc}
                        </div>
                    <br class="clearer"/>
                    </div>
                </div> 
           
                 <div class="boxContent" style="margin-top:10px">
                        {$onderbrimage}
                        </div>
              </div>
          </div>
          
          <div class="box" id="onderbrekingBoxomg">
            <div class="margin">
                <h2 class="boxTitle">Wie onderbreekt '{$party}'?</h2>
                                <div class="boxDescription">
                <p>Hier wordt weergegeven hoeveel (in percentage) alle partijen, de partij `{$party}' onbreken, dit t.o.v. het totaal aantal keren dat {$party} onderbroken is geweest.</p>
                </div>
                <div class="boxContent darkBackground">
                    <div class="columnspace twoColumns darkBackground">
                        <div class="column firstColumn">
                            <h3 class="columnTitle">Naam</h3>
                            {$onderbrlistnaamomg}
                            </div>
                        <div class="column secondColumn">
                            <h3 class="columnTitle">Percentage</h3>
                              {$onderbrlistpercomg}
                        </div>
                    <br class="clearer"/>
                    </div>
                </div> 
           
                 <div class="boxContent" style="margin-top:10px">
                        {$onderbrimageomg}
                        </div>
              </div>
          </div>

        </div>
    </body>
</html>
